package com.ideaplugin.datamanager.common;


import cn.hutool.db.meta.JdbcType;
import com.google.common.collect.Sets;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created on 2023/6/7.
 *
 * @author zhaojb
 */
public enum TableColumnTypeEnum {
    //date 日期型，string字符串型，number数字型,primary 主键
    date, string, number;

    private static Map<TableColumnTypeEnum, Set<Integer>> MAPPING;

    static {
        MAPPING = new ConcurrentHashMap<>();
        MAPPING.put(TableColumnTypeEnum.date, Sets.newHashSet(JdbcType.TIMESTAMP.typeCode, JdbcType.DATE.typeCode, JdbcType.TIME.typeCode, JdbcType.TIME_WITH_TIMEZONE.typeCode, JdbcType.TIMESTAMP_WITH_TIMEZONE.typeCode));
        //boolean也是数字
        MAPPING.put(TableColumnTypeEnum.number, Sets.newHashSet(JdbcType.NUMERIC.typeCode, JdbcType.BIGINT.typeCode, JdbcType.TINYINT.typeCode, JdbcType.DOUBLE.typeCode, JdbcType.REAL.typeCode, JdbcType.INTEGER.typeCode, JdbcType.BIGINT.typeCode, JdbcType.SMALLINT.typeCode, JdbcType.BOOLEAN.typeCode));
        MAPPING.put(TableColumnTypeEnum.string, Sets.newHashSet(JdbcType.VARCHAR.typeCode));
    }

    //java.sql.Types
    public static TableColumnTypeEnum getColumnType(Integer javaSqlType) {
        for (Map.Entry<TableColumnTypeEnum, Set<Integer>> entry : MAPPING.entrySet()) {
            if (entry.getValue().contains(javaSqlType)) {
                return entry.getKey();
            }
        }
        return TableColumnTypeEnum.string;
    }

}
